

function say() {
    alert("LC:29. 两数相除");
}
say();

/**
 * @param {number} dividend
 * @param {number} divisor
 * @return {number}
 */
var divide = function(dividend, divisor) {
    // 10:25-40
    var flag = 1
    if(dividend * divisor < 0){
        flag = -1
    }

    var absDivid = Math.abs(dividend)
    var absDivisor = Math.abs(divisor)

    if(absDivid < absDivisor) {
        return 0
    }

    var left = 0
    var right = absDivid
    while(left <= right){
        var mid = Math.floor(left + (right - left) / 2)
        if(mid * absDivisor > absDivid) {
            right = mid - 1
        }else if(mid * absDivisor == absDivid) {
            return mid * flag > 2147483647 ? 2147483647 : mid * flag
        }else { //乘积小于 被除数
            if((mid + 1) * absDivisor > absDivid) {
                return mid * flag > 2147483647 ? 2147483647 : mid * flag
            }else {
                left = mid + 1
            }
        }
    }
};
